package com.caiheng.api.service.attendance.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.caiheng.api.dao.attendance.AttendanceGroupUserMapper;
import com.caiheng.api.entity.attendance.AttendanceGroupUser;
import com.caiheng.api.entity.product.ProductCart;
import com.caiheng.api.entity.wxuser.User;
import com.caiheng.api.exception.TransactionalException;
import com.caiheng.api.service.attendance.AttendanceGroupUserService;
import com.caiheng.api.service.base.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 考勤组 成员Service业务层处理
 *
 * @author ruoyi
 * @date 2022-12-06
 */
@Service
public class AttendanceGroupUserServiceImpl extends BaseServiceImpl<AttendanceGroupUserMapper, AttendanceGroupUser> implements AttendanceGroupUserService {

    @Resource
    private AttendanceGroupUserMapper attendanceGroupUserMapper;

    @Override
    public AttendanceGroupUser getDefaultGroup(User user) {
        QueryWrapper<AttendanceGroupUser> qw = new QueryWrapper<>();
        qw.eq("sys_user_id",user.getSysUserId());
        qw.eq("state",1);
        qw.eq("is_default",1);
        qw.last("limit  1");
        AttendanceGroupUser groupUser = attendanceGroupUserMapper.selectOne(qw);
        if(groupUser == null){
            QueryWrapper<AttendanceGroupUser> qw1 = new QueryWrapper<>();
            qw1.eq("sys_user_id",user.getSysUserId());
            qw1.eq("state",1);
            qw1.last("limit  1");
            groupUser = attendanceGroupUserMapper.selectOne(qw1);
        }
        return groupUser;
    }

    @Override
    public AttendanceGroupUser getGroupByUserId(int userId, int groupId) {
        QueryWrapper<AttendanceGroupUser> qw = new QueryWrapper<>();
        qw.eq("user_id",userId);
        qw.eq("group_id",groupId);
        qw.eq("is_default", 1);
        qw.last("limit  1");
        AttendanceGroupUser groupUser = attendanceGroupUserMapper.selectOne(qw);
        return groupUser;
    }


    @Override
    public AttendanceGroupUser getGroupBySysUserId(long sysUserId, int groupId) {
        QueryWrapper<AttendanceGroupUser> qw = new QueryWrapper<>();
        qw.eq("sys_user_id", sysUserId);
        qw.eq("group_id",groupId);
//        qw.eq("is_default", 1);
        qw.last("limit  1");
        AttendanceGroupUser groupUser = attendanceGroupUserMapper.selectOne(qw);
        return groupUser;
    }

    @Override
    @Transactional
    public int changeDefaultGroup(User user, int groupId) {
        int ret = 0;
        try{
            Map<String,Object> map = new HashMap<>();
            map.put("sysUserId", user.getSysUserId());
            List<AttendanceGroupUser> list = attendanceGroupUserMapper.selectAttendanceGroupUserList(map);
            if(CollUtil.isNotEmpty(list)){
                for (AttendanceGroupUser groupUser : list){
                    //先重置 默认状态 都未0
                    ret = attendanceGroupUserMapper.update(groupUser,new LambdaUpdateWrapper<AttendanceGroupUser>()
                            .set(AttendanceGroupUser::getIsDefault, 0).eq(AttendanceGroupUser::getId,groupUser.getId()));
                    if(ret <= 0){
                        throw new TransactionalException("attendanceGroupUserMapper updateById error");
                    }
                }
            }

            AttendanceGroupUser groupUser = getGroupBySysUserId(user.getSysUserId(), groupId);
            if(groupUser != null){
                groupUser.setUserId(user.getId());
                groupUser.setIsDefault(1);
                ret = attendanceGroupUserMapper.updateById(groupUser);
                if(ret <= 0){
                    throw new TransactionalException("attendanceGroupUserMapper updateById error");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            throw new TransactionalException("attendanceMapper updateById error");
        }
        return ret;
    }

    @Override
    public AttendanceGroupUser getCheckHolidayManager(int propertyId, User user) {
        QueryWrapper<AttendanceGroupUser> qw = new QueryWrapper<>();
        qw.and(wrapper->{
            wrapper.eq("user_id", user.getId()).or().eq("sys_user_id", user.getSysUserId());
        });
        if(propertyId > 0){
            qw.eq("property_id",propertyId);
        }
        qw.eq("state",1);
        qw.eq("is_check_holiday",1);
        qw.last("limit  1");
        return attendanceGroupUserMapper.selectOne(qw);
    }

    @Override
    public List<AttendanceGroupUser> selectAttendanceGroupUserList(Map<String, Object> map) {
        return attendanceGroupUserMapper.selectAttendanceGroupUserList(map);
    }
}
